﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Public
{
    public partial class wfVehicleList2 : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        private void getArea2(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                string cityId = this.GetParamHtValue("cityId");
                this.ExecJScript(@"selectArea('secendArea'," + selectData.ToString() + ",'" + cityId + "');");
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                getArea3(cityId);
            }
        }
        private void getArea3(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                string destionId = this.GetParamHtValue("destionId");
                this.ExecJScript(@"selectArea('area'," + selectData.ToString() + ",'" + destionId + "');");

            }
        }
        private void initplace(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                StringBuilder selectData = new StringBuilder();
                selectData.Append(@"[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        selectData.Append(",");
                    }
                    string tempData = @"{""ID"":""$ID$"",""Name"":""$Name$""}";
                    tempData = tempData.Replace("$ID$", ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"].ToString());
                    tempData = tempData.Replace("$Name$", ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"].ToString());
                    selectData.Append(tempData);
                }
                selectData.Append(@"]");
                string countryId = this.GetParamHtValue("countryId");
                this.ExecJScript(@"selectArea('topArea'," + selectData.ToString() + ",'" + countryId + "');");
                if (string.IsNullOrEmpty(countryId))
                {
                    countryId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                getArea2(countryId);
            }
        }

        private void GetCarType()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle=new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicle.GetVehicleType();
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"setVehicleType(" + retJson + ")");
            }
        }

        private void GetCarBrand() {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle=new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataTable dt = vehicle.GetVehicleBrand();
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 1000, "");
                this.ExecJScript(@"setVehicleBrand(" + retJson + ")");
            }
        }

        private void GetVehicleList()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle=new AppLogic.DataAlc.CBizVehicleInfo())
            {
                int pageRows = 10;
                int.TryParse(this.GetParamHtValue("pageRows"),out pageRows);
                int pageIndex = 0;
                int.TryParse(this.GetParamHtValue("pIndex"), out pageIndex);
                string countryId=this.GetParamHtValue("countryId");
                string cityId=this.GetParamHtValue("cityId");
                string destionId=this.GetParamHtValue("destionId");
                int vehicleType=0;
                int.TryParse(this.GetParamHtValue("vehicleType"),out vehicleType);
                int brand=0;
                int.TryParse(this.GetParamHtValue("checkBrand"),out brand);
                decimal minVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("minVehiclePrice"),out minVehiclePrice);
                decimal maxVehiclePrice = 0;
                decimal.TryParse(this.GetParamHtValue("maxVehiclePrice"), out maxVehiclePrice);
                int minVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("minVehicleSeat"), out minVehicleSeat);
                int maxVehicleSeat = 0;
                int.TryParse(this.GetParamHtValue("maxVehicleSeat"), out maxVehicleSeat);
                int serviceType = 0;
                int.TryParse(this.GetParamHtValue("serviceType"), out serviceType);
                DataSet ds = vehicle.GetVehicleForPageview(pageRows, pageIndex, destionId, cityId, countryId, vehicleType, brand,minVehiclePrice,maxVehiclePrice,minVehicleSeat,maxVehicleSeat,serviceType);
                if (ds==null||ds.Tables[0].Rows.Count<1)
                {
                    string nodata = @"<tr class=""lastSearchContent""><td colspan=""4"" style=""color:#e23030; background-color:#e9ef5c;padding-left:20px;"">暂时没有您查找的数据...</td></tr>";
                    this.ExecJScript(@"$('#carList').html('" + nodata + "')");
                    return;
                }
                DataColumn dr = new DataColumn("VEHICLE_BRAND_NAME");
                ds.Tables[0].Columns.Add(dr);
                DataColumn dr1 = new DataColumn("VEHICLE_SERVICE_TYPE_NAME");
                ds.Tables[0].Columns.Add(dr1);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                   
                    ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_BRAND"].ToString()));

                    ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE"].ToString()));
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 500, "");
                            this.ExecJScript(@"
                            var _html = $('#svc_list_temp_vehicle').html();

                            laytpl(_html).render(" + retJson+ @", function (render) {
                                $('#carList').html(render);
                            });jsPage('divPage', " + Convert.ToInt32(ds.Tables[0].Rows[0]["ROWSCOUNT"].ToString()) + ", " + pageRows + ", "+pageIndex+", 'goPage');");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "initplace")
            {
                //msBsc.AppLogic.DataAlc.CBizCustomerRegister dd = new AppLogic.DataAlc.CBizCustomerRegister();
                //dd.CryCustomerPwd();
                //this.Alert("5");
                string parentsId = this.GetParamHtValue("parentsId");
                initplace(parentsId);
                GetCarType();
                GetCarBrand();
                GetVehicleList();
            }
            else if (this.strAction == "checkCountry")
            {
                string countryId = this.GetParamHtValue("countryId");
                using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
                {
                    DataSet ds = cbcc.SelectCountryByParentId(countryId);

                    string cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    this.ExecJScript(@"$('#cityId').val('" + cityId + "');refreshPage()");
                }
            }
        }
    }
}